package com.always.electrical.energy.web.data.service;

import com.always.electrical.energy.web.data.entity.SvgCurve;
import com.always.electrical.energy.web.data.mapper.SvgCurveMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class ISvgCurveService extends ServiceImpl<SvgCurveMapper, SvgCurve> implements IService<SvgCurve> {

    @Autowired
    private SvgCurveMapper svgCurveMapper;

    private final Map<String, List<String>> timeMap = new HashMap<>();

    private final Map<String, List<String>> timeMapS = new HashMap<>();

    public ISvgCurveService() {
        try (Scanner scanner = new Scanner(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("auc.txt")))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] items = line.split(",");
                timeMap.put(items[0], Arrays.asList(items));
            }
        }

        try (Scanner scanner = new Scanner(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream("auc_s.txt")))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] items = line.split(",");
                timeMapS.put(items[0], Arrays.asList(items));
            }
        }
    }

    public List<SvgCurve> selectNewList(List<BigDecimal> deviceIds) {
        return svgCurveMapper.selectNewList(deviceIds);
    }

    public  List<BigDecimal> selectAnalogDataFvr() {
        Random random = new Random();
        List<BigDecimal> list = new ArrayList<>();
        list.add(BigDecimal.valueOf(Math.round((232 + (random.nextDouble() * 3)) * 100.0) / 100.0));
        list.add(BigDecimal.valueOf(Math.round((232 + (random.nextDouble() * 3)) * 100.0) / 100.0));
        list.add(BigDecimal.valueOf(Math.round((232 + (random.nextDouble() * 3)) * 100.0) / 100.0));
        return list;
    }

    public SvgCurve selectAnalogData(BigDecimal deviceId) {
        SvgCurve svgCurve = new SvgCurve();

        LocalDateTime now = LocalDateTime.now();
        // 向下取整到最近的 5 分钟
        LocalDateTime roundedTime = now.minusMinutes(now.getMinute() % 5)
                .minusSeconds(now.getSecond())
                .minusNanos(now.getNano());
        String formattedTime = roundedTime.format(DateTimeFormatter.ofPattern("H:mm"));

        List<String> items;
        if (Objects.equals(deviceId, new BigDecimal("2024121400000146"))) {
            items = timeMap.get(formattedTime);
        } else {
            items = timeMapS.get(formattedTime);
        }

        svgCurve.setTotpf(new BigDecimal(items.get(13)));
        svgCurve.setGridAUnbalance(new BigDecimal(items.get(6)));
        svgCurve.setGridPhsA(new BigDecimal(items.get(1)));
        svgCurve.setGridPhsB(new BigDecimal(items.get(2)));
        svgCurve.setGridPhsC(new BigDecimal(items.get(3)));

        svgCurve.setLoadTotpf(new BigDecimal(items.get(14)));
        svgCurve.setLoadAUnbalance(new BigDecimal(items.get(12)));
        svgCurve.setLoadPhsA(new BigDecimal(items.get(7)));
        svgCurve.setLoadPhsB(new BigDecimal(items.get(8)));
        svgCurve.setLoadPhsC(new BigDecimal(items.get(9)));

        return svgCurve;
    }

}
